menu_list = [1, 2, 3]


# 写日记
def write_txt():
    today = input("请输入今天的日期")
    content = input("请输入日记内容")
    file_name = "日记本内容.txt"
    f = open(file_name, mode="a", encoding="utf-8")
    f.write(today + '\n')
    f.write(content + '\n')
    f.flush()
    f.close()


# 根据日期读日记
def read_txt(is_day='-1'):
    file_name = "日记本内容.txt"
    f = None
    try:
        f = open(file_name, mode="r", encoding="utf-8")
    except FileNotFoundError as e:
        raise Exception("报错了")
    content = f.readlines()
    print(content)
    f.close()


# 菜单
def menu():
    print("*" * 30)
    print("""
    欢迎使用python日记本系统
    1：记日记
    2：阅读日记
    3: 退出
    """)
    print("*" * 30)
    pass


def check_op(op):
    if not op or not op.isdigit():
        return False
    a = int(op)
    if a not in menu_list:
        return False
    return True


def menu_process(op):
    op = int(op)
    match op:
        case 1:
            write_txt()
        case 2:
            read_txt()
        case 3:
            quit_app()
        case _:  # 等于java switch 的 default
            quit_app(True)


def quit_app(is_fail=False):
    if is_fail:
        print("有异常已退出")
    else:
        print("欢迎下次使用")
    quit()


if __name__ == '__main__':
    menu()
    op = input("请输入你的选择")
    while True:
        check = check_op(op)
        if check:
            break
        print("请输入正确选择")
        menu()
        op = input("请输入你的选择")
    menu_process(op)
